//
//  AppDelegate.swift
//  SinaWeibo
//
//  Created by mac on 15/11/26.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
//       let isNew = isNewVersion()
//        
//        if isNew {
//            
//            print("OK")
//        }

        
//        程序启动可以加载的到，说明就是正确的
        let userAccount = GDMUserAccount.loadAccount()
        
        print(userAccount)
        
        
        //        调用监听通知的方法
        registerNotification()
        
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        
        window?.backgroundColor = UIColor.whiteColor()
        
//        s设置窗口为主视图，并且可见
        window?.makeKeyAndVisible()
        
//        设置window的跟控制器
        window?.rootViewController = defaultRootViewController()
        
//        设置tabBar上的渲染的颜色，设置的是全局的
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
        
        return true 
    }
    
//    根据用户的判断进行跳转不同的控制器
    private func defaultRootViewController() -> UIViewController {
        
        if GDMAccountViewModel().userLogin {
            
            if isNewVersion() {
                
                
                return GDMNewFeatureController()
                
            }
            
            return GDMWelcomeController()
        }
        return GDMMainController()
    }
    
    
//    判断是不是新版本
    private func isNewVersion() -> Bool{
        
//        获取系统版本号
        let dict = NSBundle.mainBundle().infoDictionary!
        
//        print(dict)
        
        let currentNum = (dict["CFBundleShortVersionString"] as! NSString).doubleValue
        
//        给这个版本号起一个名字,根据这个名字在系统值找对应的版本号
        let saveVersion = "saveVersion"
        
        let defaults = NSUserDefaults.standardUserDefaults()
        
//        获取上一个版本
        let lastVersion = defaults.doubleForKey(saveVersion)
//        记录当前的系统版本
        defaults.setObject(currentNum, forKey: saveVersion)
        
//        同步到沙河
        defaults.synchronize()
        
        return currentNum > lastVersion
    }
    
    //    MARK  通知的方法
    @objc private func switchRootVC(note : NSNotification){
        
//        print(note.object)
        
        //        选择控制器的跟控制器
        window?.rootViewController = note.object == nil ? GDMMainController() : GDMWelcomeController()
    }
    
//    MARK 监听通知
    private func registerNotification(){
        
//        监听通知，现在这里加一个冒号的原因是，在实现方法的时候需要一个自己，所以传递参数的时候就要加后面的冒号
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchRootVC:", name: WBSwitchRootVCNotification, object: nil)
    }
  
//    移除通知，其实移除通知在使用在这里并没有什么作用，因为这个程序是在你退出之后才会死掉的，所以移除也是在程序最后的时候才会死掉
//    deinit{
//        
//        NSNotificationCenter.defaultCenter().removeObserver(self)
//        
//    }
    
    
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

